//
//  HMEmoticonToolBar.swift
//  Weibo
//
//  Created by 黄亦寒 on 2016/10/6.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
//toolBar子控件的枚举类型
enum HMEmoticonToolBarButtonType:Int {
    //最近
    case recent = 10
    //默认
    case normal = 11
    //Emoji
    case emoji = 12
    //浪小花
    case lxh = 13
}

//自定义表情键盘的toolBar
class HMEmoticonToolBar: UIStackView {

    //记录上次选中的按钮
    var lastSelectedButton:UIButton?
    //点击子控件执行的闭包
    var callBack:((HMEmoticonToolBarButtonType)->())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setUpUI() {
        //设置布局方式
        axis = .horizontal
        //设置子控件的填充方式
        distribution = .fillEqually
        
        addChildButton(title: "最近", imageName: "compose_emotion_table_left",type: .recent)
        addChildButton(title: "默认", imageName: "compose_emotion_table_mid",type: .normal)
        addChildButton(title: "Emoji", imageName: "compose_emotion_table_mid",type: .emoji)
        addChildButton(title: "浪小花", imageName: "compose_emotion_table_right",type: .lxh)
    }
    
    //添加子控件的方法
    private func addChildButton(title:String,imageName:String,type:HMEmoticonToolBarButtonType) {
        let button = UIButton()
        //把枚举的原始值作为tag值使用
        button.tag = type.rawValue
        //添加点击事件
        button.addTarget(self, action: #selector(buttonClick(btn:)), for: .touchUpInside)
        button.setTitle(title, for: .normal)
        //设置字体颜色
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        //设置字体大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        //设置背景图片
        button.setBackgroundImage(UIImage(named:imageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named:imageName + "_selected"), for: .selected)
        //去掉点击效果
        button.adjustsImageWhenHighlighted = false
        //添加按钮
        addArrangedSubview(button)
        //默认选中默认表情按钮
        if type == .normal {
            lastSelectedButton?.isSelected = false
            button.isSelected = true
            lastSelectedButton = button
        }
    }
    
    //子控件的点击事件
    @objc private func buttonClick(btn:UIButton) {
        //如果点击的子控件和上次相同就不执行闭包
        if lastSelectedButton == btn {
            return
        }
        
        lastSelectedButton?.isSelected = false
        btn.isSelected = true
        lastSelectedButton = btn
        //根据枚举的原始值初始化枚举
        let type = HMEmoticonToolBarButtonType(rawValue: btn.tag)!
        //执行闭包
        callBack?(type)
        
    }
    
    //根据组数的索引选中对应的toolBar按钮
    func selectedButton(section:Int) {
        let button = viewWithTag(section + 10) as! UIButton
        if lastSelectedButton == button {
            return
        }
        
        lastSelectedButton?.isSelected = false
        button.isSelected = true
        lastSelectedButton = button
        print(section)
    }
    
    
}
